package com.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activity.bean.ApplicationBean;
import com.activity.bean.UserBean;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.ImageViewUtil;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ApplicationAdapter extends GroupedRecyclerViewAdapter {




    public ApplicationAdapter(Context context) {
        super(context);

    }

    public UserBean.UserstoreinfoBean userstoreinfoBean;
    private ArrayList<ApplicationBean> data = new ArrayList();


    public ArrayList<ApplicationBean> getData() {
        return data;
    }

    public void setData(ArrayList<ApplicationBean> data) {
        this.data = data;
    }


    @Override
    public boolean hasHeader(int groupPosition) {

        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        if (groupPosition == 0)
            return false;
        return true;
    }

    @Override
    public int getGroupCount() {

        return data.size() + 1;
    }

    @Override
    public int getHeaderViewType(int groupPosition) {
        if (groupPosition == 0)
            return 100;
        return super.getHeaderViewType(groupPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        if (groupPosition == 0)
            return 0;
        return data.get(groupPosition - 1).data.size();

    }

    @Override
    public int getChildViewType(int groupPosition, int childPosition) {
        return TYPE_CHILD;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        if (viewType == 100) {
            return R.layout.application_header_banner;
        }
        return R.layout.application_header;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.application_footer;
    }

    @Override
    public int getChildLayout(int viewType) {


        return R.layout.item_application;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {

        if (groupPosition == 0) {

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.itemView.findViewById(R.id.r_bg).getLayoutParams();

            WindowManager m = ((Activity) mContext).getWindowManager();
            Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
            params.height = (int) ((d.getWidth() * 244.0 / 755.0));
            holder.itemView.findViewById(R.id.r_bg).setLayoutParams(params);


            ImageView icon = holder.itemView.findViewById(R.id.iv_icon);
            TextView tvStoreName = holder.itemView.findViewById(R.id.tv_store_name);
            TextView tvPhone = holder.itemView.findViewById(R.id.tv_phone);
            TextView tvAddress = holder.itemView.findViewById(R.id.tv_address);

           if(userstoreinfoBean!=null){
               ImageViewUtil.loadCircleImage(userstoreinfoBean.getStoreicon(),icon,mContext);
               tvStoreName.setText(userstoreinfoBean.getStorename());
               tvPhone.setText(userstoreinfoBean.getStoretel());
               tvAddress.setText(userstoreinfoBean.getStoreaddress());
           }

        } else {
            holder.setText(R.id.tv_title, data.get(groupPosition - 1).title);
        }

    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

    }


    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {

        ApplicationHolder applicationHolder = (ApplicationHolder) holder;

        applicationHolder.setData(data.get(groupPosition - 1).data.get(childPosition));


    }

    //
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_CHILD) {

            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);

            return new ApplicationHolder(view);

        }
        return super.onCreateViewHolder(parent, viewType);
    }


    public class ApplicationHolder extends BaseViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;

        public ApplicationHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(ApplicationBean.ApplicationItemBean data) {
            ivIcon.setImageResource(data.icon);
            tvName.setText(data.name);
        }


    }

}
